﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_Emp1_Record1 : AdminPageBase
{
	private string SearchWord;
    private string returnURL;
    private int SearchFlag;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        if (!IsPostBack)
        {

            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "ClassRecordId");
            this.GridView1.Attributes.Add("SortDirection", "ASC");
            //GridViewBind();
			 getParam();
		    if (SearchFlag ==1)
            {
                GridViewBind();
            }
            else
           {
				DataTable dt = new DataTable();
				dt.Columns.Add("ClassRecordId");
				dt.Columns.Add("Num");
				dt.Columns.Add("Title");

				dt.Columns.Add("");

				GridviewControl.GridViewDataBind(this.GridView1, dt);
			}
            if (add == 1)
            {
               addClassRecord.Enabled = true;
               

            }
            else
            {
                addClassRecord.Enabled = false;

            }

        }
    }

    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }

    }
	private void getParam()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["SearchWord"]))
        {
            SearchWord = Request.QueryString["SearchWord"];
            txtkeyword.Text = SearchWord;
        }
        if (!string.IsNullOrEmpty(Request.QueryString["SearchFlag"]))
            SearchFlag = Convert.ToInt32(Request.QueryString["SearchFlag"]);
       
    }
    protected void GridViewBind()
    {

        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];

        string keyword = txtkeyword.Text.Trim();

        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchClassRecord(1, keyword, pager1.PageSize, pager1.CurrentPageIndex, sortExpression, sortDirection, Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"]));
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                //this.GridView1.DataSource = ds.Tables[0];
               // this.GridView1.DataBind();
                GridviewControl.GridViewDataBind(this.GridView1, ds.Tables[0]);
                pager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ClassRecordId");
                dt.Columns.Add("Num");
                dt.Columns.Add("Title");
               

                dt.Columns.Add("");
                GridviewControl.GridViewDataBind(this.GridView1, dt);
                pager1.RecordCount = 0;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtkeyword.Focus();
        }

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }

    }
    protected void pager1_PageChanged(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
		GridviewControl.ResetGridView(this.GridView1);
    }
    protected void ibtnsearch_Click(object sender, ImageClickEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
		GridviewControl.ResetGridView(this.GridView1);
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.GridViewBind();
		GridviewControl.ResetGridView(this.GridView1);
    }
    protected void addClassRecord_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Record1Detail.aspx?MenuId=" + MenuId);
        tbladdClassRecord.Style.Remove("display");
        tbladdClassRecord.Style.Add("display", "block");
        txttitle.Text = "";
        DataSet ds = jtDataAccess.GetNewNum("ClassRecord1");
        if (CoreTools.CheckDataSet(ds))
            lblNum.Text = ds.Tables[0].Rows[0]["Num"].ToString();
		 GridviewControl.ResetGridView(this.GridView1);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string Num = lblNum.Text;
        string title = txttitle.Text.Trim();
        if (CoreTools.ProcessSqlStr(title, 0))
        {
            int flag = jtDataAccess.OpClassRecord(JTDataAccess.OpType.Insert, 0,1, Num, title,Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"]),0);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("已经存在相同编号的课程,添加失败！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加失败，请重试！"), false);
                    break;
                default:
                    GridViewBind();
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加成功！"), false);
                    tbladdClassRecord.Style.Remove("display");
                    tbladdClassRecord.Style.Add("display", "none");
                    break;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txttitle.Focus();
        }
		  GridviewControl.ResetGridView(this.GridView1);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            string Num = "";
            string Title = "";
            GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
            Label lblNum = (Label)(drv.FindControl("lblNum"));
            Label lbltitle = (Label)(drv.FindControl("lblTitle"));
            if (lblNum != null)
                Num = lblNum.Text;
            if (lbltitle != null)
                Title = lbltitle.Text;
			int flag = 0;
			if (GridView1.Rows.Count > 0)
				flag = 1;
			returnURL ="Record1.aspx?MenuId="+MenuId+"&SearchWord=" + txtkeyword.Text.Trim()+ "&SearchFlag=" +flag;
            Response.Redirect("Record1Detail.aspx?id=" + e.CommandArgument.ToString() +"&Num="+Num+"&Title="+Title+"&returnURL="+Server.UrlEncode(returnURL));
        }
        else if (e.CommandName == "Del")
        {
            int flag = 0;
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            flag = jtDataAccess.OpClassRecord(JTDataAccess.OpType.Delete, id, 1, "", "", 0,0);

            switch (flag)
            {

                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("该记录已经在使用，不允许删除！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                    this.GridViewBind();
                    break;
            }
        }
		  GridviewControl.ResetGridView(this.GridView1);
    }

}